package com.psm.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.psm.interceptor.IndexInterceptor;
import com.psm.model.DataGrid;
import com.psm.model.PuPosts;
import com.psm.model.PuTerm;
import com.psm.model.SysPaCode;
import com.psm.model.SysRole;

/**
 * 
 * @author 广州朴石茗文化发展有限公司
 * @Email pushiming@sina.cn
 */
@Before(IndexInterceptor.class)
public class PostsController extends BaseController<PuPosts>{

	public Logger log=Logger.getLogger(getClass());

    public void index(){
        render("index.html");
    }

    public void term(){
        render("term.html");
    }
    /**
     * 文章列表
     */
    public void listJson(){

        DataGrid dg = new DataGrid();

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        String name = this.getPara("name", "");

        where.append(" where 1=1 ");


        if(!"".equals(name)){
            where.append(" and name like '%" + name + "%'");
        }

        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }

        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select * ", "from " + PuPosts.tableName + where.toString(), param.toArray());

        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);

    }
    /**
     * 增加文章
     */
    public void postadd(){
    	try {
            Long id = getParaToLong("id");
            PuPosts post = PuPosts.me.findById(id);
            this.setAttr("po",post);
        } catch (Exception e) {
            log.error("编辑异常", e);
        }
    }
    /**
     * 保存文章
     */
    public void postsave(){
    	String post_title = getPara("post_title");
    	String post_keywords = getPara("post_keywords");
    	String post_zuozhe = getPara("post_zuozhe");
    	String post_content = getPara("post_content");
    	String post_modified = getPara("post_modified");
    	String post_type = getPara("post_type");
    	
    	String id = getPara("id", "");
    	PuPosts post = null;
    	 if("".equals(id)){
    		 post = new PuPosts();
    		 post = this.newPuPost(0, post, post_type, post_title, post_keywords, post_zuozhe, post_modified, post_content);
             post.save();
    	 }else{
    		 post = PuPosts.me.findById(id);
    		 post = this.newPuPost(0, post, post_type, post_title, post_keywords, post_zuozhe, post_modified, post_content);
    		 post.update();
    	 }
    	 this.rendJson_(1, "操作成功");
    	 
    }
    
    public PuPosts newPuPost(int type,PuPosts post,String post_type,String post_title,String post_keywords,String post_zuozhe,String post_modified,String post_content){
    	
    	post.set("post_title", post_title);
        post.set("post_keywords", post_keywords);
        post.set("post_zuozhe", post_zuozhe);
        post.set("post_modified", post_modified);
        post.set("post_content", post_content);
        post.set("post_type", post_type);
        
		Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        post.set("post_date", timeStamp);
        post.set("post_author",getCurrentUserId());
			
    	return post;
    }

    
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
